package com.xinchao.controller;

import com.xinchao.core.constant.DeviceModelConstant;
import com.xinchao.core.constant.ResponseConstant;
import com.xinchao.core.exception.ServiceException;
import com.xinchao.core.util.SimpleExportUtil;
import com.xinchao.model.DeviceModel;
import com.xinchao.model.sys.JsonResponse;
import com.xinchao.model.vo.DeviceModelExportListVO;
import com.xinchao.model.vo.DeviceModelSelectListVO;
import com.xinchao.service.DeviceModelService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

/**
 * 终端机型
 *
 * @author linqiang
 * @date 2018-08-07 14:47
 */
@Controller
@RequestMapping(value = "/deviceModel")
public class DeviceModelController {
    private Logger logger = LoggerFactory.getLogger(DeviceModelController.class);
    @Autowired
    private DeviceModelService deviceModelService;
    @Autowired
    private  SimpleExportUtil SimpleExportUtil;

    /**
     * 终端机型分页列表
     *
     * @return Object
     */
    @RequestMapping(value = "/listByPage")
    public Object index() {
        return "device_model_list";
    }

    @ResponseBody
    @RequestMapping(value = "/listdata")
    public Object listData(Integer pageNum, Integer pageSize,String deviceModel,String mainboard,String brand,Boolean isExist4G ) {
        return deviceModelService.listDeviceModelByPage(pageNum, pageSize,deviceModel,mainboard,brand,isExist4G);
    }

    /**
     * 新增终端机型
     *
     * @param deviceModel 终端机型信息
     * @param request     请求
     * @return Object
     */
    @ResponseBody
    @RequestMapping(value = "/addDeviceModel")
    public Object addDeviceModel(DeviceModel deviceModel, HttpServletRequest request) {
        if (deviceModel == null) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_NO_PARAM);
        }
        if (StringUtils.isBlank(deviceModel.getDeviceModel())) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_NO_PARAM);
        }
        if(StringUtils.isNotBlank(deviceModel.getRemark())){
            if(deviceModel.getRemark().length() > DeviceModelConstant.MAX_REMARK_LENGTH){
                return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_REMARK_LENGTH_OVER);
            }
        }
        try {
            deviceModelService.addDeviceModel(deviceModel, request);
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, DeviceModelConstant.MSG_ADD_DDEVICE_MODEL_SUECCESS);
        } catch (Exception e) {
            logger.error("DeviceModelController addDeviceModel 新增终端机型失败：" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_ADD_DDEVICE_MODEL_FAIL);
    }

    /**
     * 删除终端型号
     *
     * @param deviceModelIds 终端型号Id(多个Id之间用逗号隔开 )
     * @return Object
     */
    @ResponseBody
    @RequestMapping(value = "/batchDeleteDeviceModel")
    public Object batchDeleteDeviceModel(String deviceModelIds, HttpServletRequest request) {
        if (StringUtils.isBlank(deviceModelIds)) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_NO_PARAM);
        }
        try {
            deviceModelService.deleteDeviceModelBatch(deviceModelIds, request);
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, DeviceModelConstant.MSG_DELETE_DDEVICE_MODEL_SUCCESS);
        } catch (ServiceException e1) {
            e1.printStackTrace();
            logger.error("DeviceModelController batchDeleteDeviceModel 批量删除终端型号失败：" + e1.getMessage());
            return new JsonResponse(ResponseConstant.CODE_FAIL, e1.getMessage());
        } catch (Exception e) {
            logger.error("DeviceModelController batchDeleteDeviceModel 批量删除终端型号失败：" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_DELETE_DDEVICE_MODEL_FAIL);
    }

    /**
     * 修改终端机型
     *
     * @param deviceModel 终端机型信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateDeviceModel")
    public Object updateDeviceModel(DeviceModel deviceModel, HttpServletRequest request) {
        if (deviceModel == null || deviceModel.getDeviceModelId() == null) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_NO_PARAM);
        }
        if (StringUtils.isBlank(deviceModel.getDeviceModel())) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_NO_PARAM);
        }
        if(StringUtils.isNotBlank(deviceModel.getRemark())){
            if(deviceModel.getRemark().length() > DeviceModelConstant.MAX_REMARK_LENGTH){
                return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_REMARK_LENGTH_OVER);
            }
        }
        try {
            deviceModelService.updateByPrimaryKeySelective(deviceModel, request);
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, DeviceModelConstant.MSG_UPDATE_DDEVICE_MODEL_SUCCESS);
        } catch (ServiceException e1) {
            e1.printStackTrace();
            logger.error("DeviceModelController updateDeviceModel 修改终端机型失败：" + e1.getMessage());
            return new JsonResponse(ResponseConstant.CODE_FAIL, e1.getMessage());
        } catch (Exception e) {
            logger.error("DeviceModelController updateDeviceModel 修改终端机型失败：" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_UPDATE_DDEVICE_MODEL_FAIL);
    }

    /**
     * 根据主键获取单个机型
     *
     * @param deviceModelId 终端机型信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getDeviceModelById")
    public Object getDeviceModelById(Long deviceModelId) {
        if (deviceModelId == null) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_NO_PARAM);
        }
        try {
            DeviceModel deviceModel = deviceModelService.getDeviceModelById(deviceModelId);
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, deviceModel, DeviceModelConstant.MSG_SELECT_SUCCESS);
        } catch (Exception e) {
            logger.error("DeviceModelController getDeviceModelById 获取终端机型失败：" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_SELECT_FAIL);
    }

    /**
     * 获取机型下拉集合
     *
     * @return Object
     */
    @ResponseBody
    @RequestMapping(value = "/getDeviceModelSelectList")
    public Object getDeviceModelSelectList() {
        try {
            List<DeviceModelSelectListVO> deviceModelSelectListVOList = deviceModelService.selectList();
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, deviceModelSelectListVOList, DeviceModelConstant.MSG_SELECT_SUCCESS);
        } catch (Exception e) {
            logger.error("DeviceModelController getDeviceModelSelectList 获取终端机型下拉集合失败：" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_SELECT_FAIL);
    }


    /**
     * 终端机型导出
     *
     * @param response 响应
     */
    @RequestMapping("/export")
    public void export(HttpServletResponse response,String deviceModel,String mainboard,String brand,Boolean isExist4G) {
        try (OutputStream out = response.getOutputStream()) {
            List<DeviceModelExportListVO> exportList = deviceModelService.listExport(deviceModel, mainboard, brand,isExist4G);
            //设置输出类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置文件名
            response.setHeader("Content-Disposition", "attachment;filename="
                    + new String((DeviceModelConstant.EXPORT_FILE_NAME).getBytes(), "ISO-8859-1"));
            SimpleExportUtil.exportToExcel(exportList, DeviceModelConstant.EXPORT_SHEET_NAME).write(out);
        } catch (Exception e) {
            logger.error("DeviceModelController export 导出终端机型失败：" + e.getMessage());
        }
    }

}
